//
//  NSString+Json.h
//  Titop
//
//  Created by l.t.zero on 15/7/24.
//  Copyright (c) l.t.zero. All rights reserved.
//


#import "NSString+Json.h"

@implementation NSString (Json)

- (NSDictionary *)dictionaryValue {
    if(![self isKindOfClass:[NSString class]]
       || self.length == 0){
        return nil;
    }
    
    if([self isKindOfClass:[NSDictionary class]]){
        return (NSDictionary*)self;
    }
    
    NSData *JSONData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    return responseJSON;
}

+ (NSString *)stringFromDictionary:(NSDictionary *)dic{
    if (!dic) return @"";
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return !parseError?[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]:@"";
}
@end
